package com.weiaibenpao.demo.chislim.adater;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.Li_SportHistory_Result;
import com.weiaibenpao.demo.chislim.util.DateUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by lenovo on 2017/2/11.
 */

public class DateRemberAdapter extends RecyclerView.Adapter<DateRemberAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    Context context;
    ArrayList dataList;

    ArrayList monthList;
    HashMap<String,String> hashMap;
    DateUtil dateUtil;
    DecimalFormat decimalFormat;

    ArrayList newArrayList;   //临时的集合

    double valueMonth; //当天的值
    double oldMoth;  //累计的值

    String month;

    private static OnItemClickListener mOnItemClickListener;
    //设置点击和长按事件
    public interface OnItemClickListener {
        void onItemClick(View view, ArrayList monthList, HashMap<String,String> hashMap);        //设置点击事件
    }
    public static void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /**
     * 构造器
     * @param context
     */
    public DateRemberAdapter(Context context, ArrayList arrayList) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        dataList = arrayList;

        monthList = new ArrayList();
        hashMap = new HashMap<String,String>();
        dateUtil = new DateUtil();
        if (decimalFormat == null) {
            decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        }

        newArrayList = new ArrayList();

        for(int i = 0;i < dataList.size();i++){
            //字符串转日期
            //格式化日期
            try {
                month = dateUtil.getTime(((Li_SportHistory_Result.DataBean.ThemelistBean) dataList.get(i)).getSportDate());
                valueMonth = (((Li_SportHistory_Result.DataBean.ThemelistBean)arrayList.get(i)).getTotalDistance())/1000;
                //如果集合中已经有这个月份的值
                Log.i("累计", "--是否集合中有这个月份--" + monthList.contains(month));
                if (monthList.contains(month)) {
                    //拿到累计的月份
                    oldMoth = Double.parseDouble(hashMap.get(month));
                    //将今天的加进去，放到hashMap中
                    hashMap.put(month, decimalFormat.format(oldMoth + valueMonth));
                    Log.i("累计", "--累计--" + hashMap.get(month));
                } else {
                    //将月份的值添加到List中
                    monthList.add(month);
                    //如果集合中没有这个月份的值，那么添加这个月份
                    hashMap.put(month, decimalFormat.format(valueMonth));
                    Log.i("累计", "--累计--" + hashMap.get(month));
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * item显示类型
     * 引入布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_monthrember, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 数据的绑定显示
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if(position%2 == 0){
            holder.viewLayout.setBackgroundColor(Color.parseColor("#CC358F"));
        }else{
            holder.viewLayout.setBackgroundColor(Color.parseColor("#18A0F6"));
        }

        holder.myText.setText(monthList.get(position) + "月份累计跑步" + hashMap.get(monthList.get(position)) + "公里");

        //绑定事件
        if (mOnItemClickListener != null) {
            holder.myText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(holder.myText, monthList,hashMap);
                }
            });
        }
    }

    /**
     * 要显示的item数目
     * @return
     */
    @Override
    public int getItemCount() {
        return monthList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView myText;
        public LinearLayout viewLayout;

        public ViewHolder(View view) {
            super(view);
            myText = (TextView) view.findViewById(R.id.myText);
            viewLayout = (LinearLayout) view.findViewById(R.id.viewLayout);
        }
    }
}